package com.etc.controller;

import com.etc.entity.Counthhf;
import com.etc.entity.User;
import com.etc.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/saveUser")
    public String saveUser(User user, String pdate1, String lastdate1) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date pdate = sdf.parse(pdate1);
        Date lastdate = sdf.parse(lastdate1);
        user.setPdate(pdate);
        user.setLastdate(lastdate);
        System.out.println(user);
        userService.saveUser(user);
        return "redirect:/user/queryAllUser";
    }

    @RequestMapping("/queryAllUser")
    public String queryAllUser(String pageNum,Model model){
        //每页条数
        int pageSize = 5;
        //判断当前页是否为空
        if (pageNum == null) {
            pageNum = "1";
        }
        Integer ipageNum = Integer.valueOf(pageNum);
        PageHelper.startPage(ipageNum,pageSize);
        PageInfo<User> pageInfo = userService.queryAllUser();
        List<User> users = pageInfo.getList();
//        for(User u:users){
//            System.out.println(u.getRole().getRolename());
//
//        }
        Counthhf count = userService.queryCount();
        int total = count.getCount() % pageSize == 0 ? count.getCount() / pageSize : count.getCount() / pageSize + 1;
//        System.out.println(count.getCount());
        model.addAttribute("count",count);
        model.addAttribute("total",total);
        model.addAttribute("ipageNum",ipageNum);
        model.addAttribute("users",users);
        return "userList";
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(@RequestBody User user){
//        System.out.println(user.getUserid());
        userService.deleteUser(user.getUserid());
        return "redirect:/user/queryAllUser";
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public User updateUser(@RequestBody User user){
//        System.out.println(user.getUserid());
        User people = userService.updateUser(user.getUserid());
//        System.out.println(people);
        return people;
    }

    @RequestMapping("/updateUser2")
    public String updateUser2(User user, String pdate1, String lastdate1) throws ParseException {
        System.out.println(user.getUserid());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date pdate = sdf.parse(pdate1);
        Date lastdate = sdf.parse(lastdate1);
        user.setPdate(pdate);
        user.setLastdate(lastdate);

        System.out.println(user);
        userService.updateUser2(user);

        return "redirect:/user/queryAllUser";
    }

    @RequestMapping("/queryByNamePhone")
    public String queryByNamePhone(User user, String pageNum, Model model){
        System.out.println(user.getUsername()+user.getPhone()+pageNum);
        //每页条数
        int pageSize = 5;
        //判断当前页是否为空
        if (pageNum == null) {
            pageNum = "1";
        }
        Integer ipageNum = Integer.valueOf(pageNum);
        PageHelper.startPage(ipageNum,pageSize);
        List<User> users = userService.queryByNamePhone(user);
        System.out.println("C:"+users);
        Counthhf count = userService.quserCountByNamePhone(user.getUsername(),user.getPhone());
        int total = count.getCount() % pageSize == 0 ? count.getCount() / pageSize : count.getCount() / pageSize + 1;
        System.out.println(count.getCount());
        model.addAttribute("count",count);
        model.addAttribute("total",total);
        model.addAttribute("ipageNum",ipageNum);
        model.addAttribute("users",users);
        return "userList";
    }

}
